package com.example.light.web.personal.service.impl;

import com.example.light.web.personal.dao.NameCardMapper;
import com.example.light.web.personal.entity.NameCard;
import com.example.light.web.personal.service.NameCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.Date;
import java.util.List;

@Service
public class NameCardServiceImpl implements NameCardService {
    @Autowired
    private NameCardMapper nameCardMapper;

    @Override
    public void showNamecards(Integer userId, Model model) {
        List<NameCard> nameCards =
                nameCardMapper.selectNameCards(userId);
        model.addAttribute("nameCards",nameCards);
    }

    @Override
    public boolean insert(NameCard nameCard, Integer userId) {
        nameCard.setUserId(userId);
        nameCard.setCardManipulateTime(new Date());
        nameCardMapper.insertSelective(nameCard);
        return true;
    }

    @Override
    public void selectOne(Integer id, Model model) {
        NameCard nameCard = nameCardMapper.selectByPrimaryKey(id);
        model.addAttribute("nameCard",nameCard);
    }

    @Override
    public boolean update(NameCard nameCard, Integer userId) {
        nameCard.setCardManipulateTime(new Date());
        nameCard.setUserId(userId);
        nameCardMapper.updateByPrimaryKeySelective(nameCard);
        return true;
    }

    @Override
    public boolean del(Integer nameCardId) {
        nameCardMapper.deleteByPrimaryKey(nameCardId);
        return true;
    }
}
